qx.Class.define("bip.ui.topics.ManageTopics",
{
  extend : qx.ui.core.Widget,
  include : [qx.ui.form.MModelProperty, bip.ui.form.MActionBar],
  
  /*
	*****************************************************************************
	 EVENTS
	*****************************************************************************
	*/  
	events :
	{
		/**
		 * Fired when an action has been executed successfully
		 * The data contains the model related to the action
		 */
		"success" : "qx.event.type.Data",
		
		/**
		 * Fired when an error occurs when trying to execute an action
		 * The data contains the error
		 */
    "error" : "qx.event.type.Data",
    
    /**
     * Fired at the begining of an action
     * The data contains the description of the action being executed
     */
    "running": "qx.event.type.Data"
		
	},
	
	/*
	*****************************************************************************
	 CONSTRUCTOR
	*****************************************************************************
	*/
  construct : function()
  {
    this.base(arguments);
    
    // initialize the layout
    var layout = new qx.ui.layout.VBox(5);
    this._setLayout(layout);
    
    // Create forms
    this.__propertiesForm = new bip.ui.topics.NewTopicForm(true);
    this.__addQuestionForm = new bip.ui.questions.NewQuestionForm(true);
    
    // create the widgets
    this._createChildControl("topics");
    this._createChildControl("insert-topic");
    this._createChildControl("delete-topic");
    this._createChildControl("questions");
    this._createChildControl("insert-question");
    this._createChildControl("delete-question");
    
    // Logic
    this.getChildControl("topics").getController().addListener("changeSelection",function(evt) {
    	if (evt.getData().length > 0) {
    		// At least 1 topic selected => enable topics actions
    		this.enableTopicActions(true);
    		// Fill up the questions list
    		this.getChildControl("questions").setTopic(evt.getData().getItem(0));
    	} else {this.enableTopicActions(false); }
    },this);
    this.getChildControl("questions").getController().addListener("changeSelection",function(evt) {
    	if (evt.getData().length > 0) {
    		// At least 1 question selected => enable questions actions
    		this.enableQuestionActions(true);
    	} else {this.enableQuestionActions(false); }
    },this);
    
    this.enableTopicActions(false);
    this.enableQuestionActions(false);
    
    // Events dispatching
    // Topics list
		var service = bip.services.SynchronizedTopics.getInstance();
		service.addListener("error",function(evt) { this.fireDataEvent("error",evt.getData())},this);
		service.addListener("loaded",function(evt) { this.fireDataEvent("success",evt.getData())},this);
		service.addListener("loading",function(evt) { this.fireDataEvent("running",evt.getData())},this);
		// Topics actions
		service = bip.AdminView.getInstance().getTopicService();
		service.addListener("error",function(evt) { this.fireDataEvent("error",evt.getData())},this);
		service.addListener("loaded",function(evt) { this.fireDataEvent("success",evt.getData());},this);
		service.addListener("loading",function(evt) { this.fireDataEvent("running",evt.getData())},this);
    // Question list
		service = this.getChildControl("questions").getService();
		service.addListener("error",function(evt) { 
			this.fireDataEvent("error",evt.getData())},this
		);
		service.addListener("loaded",function(evt) { 
			this.fireDataEvent("success",evt.getData());
			this.getChildControl("questions-group").setEnabled(true);
		},this);
		service.addListener("loading",function(evt) { 
			this.getChildControl("questions-group").setEnabled(false);
			this.fireDataEvent("running",evt.getData())
		},this);
		// Question actions
		this.__questionService = new bip.services.QuestionService(); 
		this.__questionService.addListener("loading",function(evt) {this.fireDataEvent("running",evt.getData());},this);
		this.__questionService.addListener("loaded",function(evt) {
			this.fireDataEvent("success",evt.getData());
			var topics = this.getChildControl("topics").getController().getSelection();
			if (topics.length > 0) {
				this.getChildControl("questions").setTopic(null);
				this.getChildControl("questions").setTopic(topics.getItem(0));
			}
		},this);
		this.__questionService.addListener("error",function(evt) {this.fireDataEvent("error",evt.getData());},this);
  },

  /*
	 * ****************************************************************************
	 * PROPERTIES
	 * ****************************************************************************
	 */
  properties :
  {
  	appearance :
    {
      refine : true,
      init : "manage-topics"
    }
  },
  
  /*
	 * ****************************************************************************
	 * MEMBERS
	 * ****************************************************************************
	 */
  members :
  { 
  	__winNewQuestion:null,
  	__questionService:null,
	  
	  /**
	   * Enable or disable actions that are possible to perform on a selected topic
	   * @param enabled {Boolean} True to enable
	   */
	  enableTopicActions: function(enabled) {
	  	this.getChildControl("delete-topic").setEnabled(enabled);
	  	this.getChildControl("questions-group").setEnabled(enabled);
	  	this.enableQuestionActions(enabled);
	  },
	  
	  /**
	   * Enable or disable actions that are possible to perform on a selected question
	   * @param enabled {Boolean} True to enable
	   */
	  enableQuestionActions: function(enabled) {
	  	this.getChildControl("delete-question").setEnabled(enabled);
	  },
	  
	  getWinNewQuestion: function() {
			if (! this.__winNewQuestion) {
				this.__winNewQuestion = new bip.ui.questions.WinNewQuestionForm(this.__questionService);
				bip.AdminView.getInstance().getDesktop().add(this.__winNewQuestion);
			}
			return this.__winNewQuestion;
		},
	   
    // overridden
    _createChildControlImpl : function(id)
    {
      var control;

      switch(id)
      {      
      	case "topics":
      		control = new bip.ui.form.SynchronizedTopicList();
      		control.setHeight(90);
      		this.getChildControl("topics-group").add(control,{row:0,column:0});
          break;
         
      	case "topics-group":
      		control = new qx.ui.groupbox.GroupBox("Select a topic or create a new one");
      		var layout = new qx.ui.layout.Grid(5,5);
      		control.setLayout(layout);
      		layout.setColumnFlex(0,1);
          this._add(control);
          break;
          
      	case "delete-topic":
      		control = bip.ui.form.PredefinedButtons.newDeleteButton("Delete topic");
      		control.addListener("execute",function() {
      			var model = this.getChildControl("topics").getController().getSelection().getItem(0);
      			bip.AdminView.getInstance().getTopicService().removeTopic(model);
      		},this);
      		this.getChildControl("topic-actions").add(control);
          break;
          
      	case "insert-topic":
      		control = bip.ui.form.PredefinedButtons.newAddButton("New topic");
      		control.addListener("execute",function() {
      			bip.AdminView.getInstance().getWinNewTopic().open();
      		},this);
      		this.getChildControl("topic-actions").add(control);
          break;
          
      	case "topic-actions":
      		control = new qx.ui.container.Composite(new qx.ui.layout.VBox(5));
      		this.getChildControl("topics-group").add(control,{row:0,column:1});
      	  break;
      	  
      	case "questions-group":
      		control = new qx.ui.groupbox.GroupBox("Manage questions of the selected topic");
      		var layout = new qx.ui.layout.Grid(5,5);
      		control.setLayout(layout);
      		layout.setColumnFlex(0,1);
          this._add(control);
          break;
          
      	case "questions":
      		control = new bip.ui.form.QuestionList(this.__questionListService);
      		control.setHeight(90);
      		this.getChildControl("questions-group").add(control,{row:0,column:0});
          break;
          
      	case "question-actions":
      		control = new qx.ui.container.Composite(new qx.ui.layout.VBox(5));
      		this.getChildControl("questions-group").add(control,{row:0,column:1});
      	  break;
          
      	case "insert-question":
      		control = bip.ui.form.PredefinedButtons.newAddButton("New question");
      		control.addListener("execute",function() {
      			var model = this.getChildControl("topics").getController().getSelection().getItem(0);
      			this.getWinNewQuestion().open({idtopic:model.getIdTopic(),idlocal:model.getIdLocal()});
      		},this);
      		this.getChildControl("question-actions").add(control);
          break;
          
      	case "delete-question":
      		control = bip.ui.form.PredefinedButtons.newDeleteButton("Delete question");
      		control.addListener("execute",function() {
      			var model = this.getChildControl("questions").getController().getSelection().getItem(0);
      			//this.debug(qx.dev.Debug.debugProperties(model));
      			this.__questionService.removeQuestion(model);
      		},this);
      		this.getChildControl("question-actions").add(control);
          break;
      		
      }

      return control || this.base(arguments, id);
    }
    
    
  },
  
  /*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */
  destruct : function()
  {
	  this._disposeObjects("__winNewQuestion","__questionService");
  }
});